﻿using Microsoft.EntityFrameworkCore;
using MovieDB.Models; // 根据您的实际命名空间调整
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// DTO 类
public class MovieParticipantDto
{
    public string ArtistName { get; set; }
    public string RoleName { get; set; }
    public string MovieName { get; set; }
}

public class ArtistMovieDto
{
    public string MovieName { get; set; }
    public string RoleName { get; set; }
    public DateOnly? ReleaseDate { get; set; }
    public float? Rating { get; set; }
}

public class MovieService
{
    private readonly MovieDbContext _context;

    public MovieService(MovieDbContext context)
    {
        _context = context;
    }

    // ========== 电影相关操作 ==========

    // 增加电影
    public async Task<Movie> AddMovieAsync(string name, DateTime releaseDate, int duration,
                                         string language, string introduction, float rating)
    {
        var movie = new Movie
        {
            Name = name,
            Releasedate = DateOnly.FromDateTime(releaseDate),
            Duration = duration,
            Language = language,
            Introduction = introduction,
            Rating = rating
        };

        await _context.Movies.AddAsync(movie);
        await _context.SaveChangesAsync();
        return movie;
    }

    

    // 查询所有电影
    public async Task<List<Movie>> GetAllMoviesAsync()
    {
        return await _context.Movies.ToListAsync();
    }

    // 根据ID查询电影
    public async Task<Movie?> GetMovieByIdAsync(int id)
    {
        return await _context.Movies.FindAsync(id);
    }

    // 根据名称模糊查询电影
    public async Task<List<Movie>> GetMoviesByNameAsync(string name)
    {
        return await _context.Movies
            .Where(m => m.Name.Contains(name))
            .ToListAsync();
    }

    // 根据评分查询电影
    public async Task<List<Movie>> GetMoviesByRatingAsync(float minRating)
    {
        return await _context.Movies
            .Where(m => m.Rating >= minRating)
            .OrderByDescending(m => m.Rating)
            .ToListAsync();
    }

    // 更新电影信息
    public async Task<bool> UpdateMovieAsync(int movieId, string? name = null, float? rating = null,
                                           string? introduction = null)
    {
        var movie = await _context.Movies.FindAsync(movieId);
        if (movie == null) return false;

        if (!string.IsNullOrEmpty(name)) movie.Name = name;
        if (rating.HasValue) movie.Rating = rating.Value;
        if (!string.IsNullOrEmpty(introduction)) movie.Introduction = introduction;

        await _context.SaveChangesAsync();
        return true;
    }

    // 删除电影
    public async Task<bool> DeleteMovieAsync(int movieId)
    {
        var movie = await _context.Movies.FindAsync(movieId);
        if (movie == null) return false;

        _context.Movies.Remove(movie);
        await _context.SaveChangesAsync();
        return true;
    }

    // ========== 艺人相关操作 ==========

    // 增加艺人
    public async Task<Artist> AddArtistAsync(string name, DateTime? birthday, string sex,
                                           string imdbNumber, string introduction)
    {
        var artist = new Artist
        {
            Name = name,
            Birthday = birthday.HasValue ? DateOnly.FromDateTime(birthday.Value) : null,
            Sex = sex,
            Imdbnumber = imdbNumber,
            Introduction = introduction
        };

        await _context.Artists.AddAsync(artist);
        await _context.SaveChangesAsync();
        return artist;
    }

    // 查询所有艺人
    public async Task<List<Artist>> GetAllArtistsAsync()
    {
        return await _context.Artists.ToListAsync();
    }

    // 根据名称查询艺人
    public async Task<List<Artist>> GetArtistsByNameAsync(string name)
    {
        return await _context.Artists
            .Where(a => a.Name.Contains(name))
            .ToListAsync();
    }

    // 更新艺人信息
    public async Task<bool> UpdateArtistAsync(int artistId, string? name = null,
                                            string? introduction = null)
    {
        var artist = await _context.Artists.FindAsync(artistId);
        if (artist == null) return false;

        if (!string.IsNullOrEmpty(name)) artist.Name = name;
        if (!string.IsNullOrEmpty(introduction)) artist.Introduction = introduction;

        await _context.SaveChangesAsync();
        return true;
    }

    // 删除艺人
    public async Task<bool> DeleteArtistAsync(int artistId)
    {
        var artist = await _context.Artists.FindAsync(artistId);
        if (artist == null) return false;

        _context.Artists.Remove(artist);
        await _context.SaveChangesAsync();
        return true;
    }

    // ========== 参与关系相关操作 ==========

    // 添加参与关系
    public async Task<Participation> AddParticipationAsync(int artistId, int movieId, string roleName)
    {
        var participation = new Participation
        {
            Artistid = artistId,
            Movieid = movieId,
            Rolename = roleName
        };

        await _context.Participations.AddAsync(participation);
        await _context.SaveChangesAsync();
        return participation;
    }

    // 获取电影的所有参与者 - 返回强类型 DTO
    public async Task<List<MovieParticipantDto>> GetMovieParticipantsAsync(int movieId)
    {
        return await _context.Participations
            .Where(p => p.Movieid == movieId)
            .Include(p => p.Artist)
            .Include(p => p.Movie)
            .Select(p => new MovieParticipantDto
            {
                ArtistName = p.Artist.Name,
                RoleName = p.Rolename,
                MovieName = p.Movie.Name
            })
            .ToListAsync();
    }

    // 获取艺人的所有电影 - 返回强类型 DTO
    public async Task<List<ArtistMovieDto>> GetArtistMoviesAsync(int artistId)
    {
        return await _context.Participations
            .Where(p => p.Artistid == artistId)
            .Include(p => p.Movie)
            .Select(p => new ArtistMovieDto
            {
                MovieName = p.Movie.Name,
                RoleName = p.Rolename,
                ReleaseDate = p.Movie.Releasedate,
                Rating = p.Movie.Rating
            })
            .ToListAsync();
    }

    // 验证参与关系是否存在
    public async Task<bool> ParticipationExistsAsync(int artistId, int movieId)
    {
        return await _context.Participations
            .AnyAsync(p => p.Artistid == artistId && p.Movieid == movieId);
    }

    // 删除参与关系
    public async Task<bool> DeleteParticipationAsync(int participationId)
    {
        var participation = await _context.Participations.FindAsync(participationId);
        if (participation == null) return false;

        _context.Participations.Remove(participation);
        await _context.SaveChangesAsync();
        return true;
    }
}